package com.dubbo.userquery;
import javax.swing.*;  

import org.json.JSONException;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.awt.*;  
import java.awt.event.*;  
import java.lang.reflect.Method;
import com.dubbo.userquery.DemoService;

public class UserQueryDemo extends Frame implements ActionListener{

	JTextArea text_array[];
	JLabel label_array[];
	JLabel user_id_label;
	JLabel user_num_label,update_num_label,insert_num_label;
	JLabel user_num_text,update_num_text,insert_num_text;
	JLabel output_label;
	JTextArea user_id_text;
	JTextArea batch_text;
    JPanel panel;
    JPanel batch_panel;
    JButton query_button_array[],update_button_array[];
    JButton add_button,del_button,batch_button;
    static final String [] label_name  = {"客户状态","认证级别","通知渠道","客户等级"};
    static final String [] get_method_name  = {"GetUserStatus","GetUserAuthentype","GetUserSource","GetUserLevel"};
    static final String [] update_method_name  = {"UpdateUserStatus","UpdateUserAuthentype","UpdateUserSource","UpdateUserLevel"};
    static DemoService service_caller;
    
	    public static void main(String[] args)  
	    {  
	    	initDubboService();
	    	new UserQueryDemo();
	    }  
	    
	    static void initDubboService()
	    {
	    	ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("conf/dubbo-consumer.xml" );  
	        context.start();  
	        try{
	           service_caller = (DemoService) context.getBean("demoService"); 
	        }
	        catch(Exception e) 
	        {
	        	
	        }
	    }
	    
	    UserQueryDemo()
	    {    	
	    	super("客户配置信息查询");  
	        this.setFont(new Font("宋体",Font.BOLD,100)); 
	        this.addWindowListener(new TestWindowListener());
	        this.setExtendedState(MAXIMIZED_BOTH);
	        
	        panel=new JPanel(); 
	        batch_panel =new JPanel();
	        
	        query_button_array=new JButton[4]; 
	        update_button_array=new JButton[4]; 
	        label_array = new JLabel[4];
	        text_array = new JTextArea[4];
	        
	        user_id_label = new JLabel();
	        user_num_label = new JLabel();
	        user_num_text = new JLabel();
	        update_num_text = new JLabel();
	        insert_num_text = new JLabel();
	        update_num_label = new JLabel();
	        insert_num_label = new JLabel();
	        
	        add_button = new JButton("新建客户");
	        del_button = new JButton("删除客户");
	        batch_button = new JButton("批量修改");
	        user_id_label.setText("客户ID");
	        user_id_text = new JTextArea(1,11);
	        batch_text = new JTextArea(30,50);
	        
	        panel.add(user_id_label);
	        panel.add(user_id_text);
	        
	        for(int i = 0; i < 4; i++)
	        {
	        	label_array[i] = new JLabel();
	        	text_array[i] = new JTextArea(1,2);
	        	query_button_array[i] = new JButton("查询");
	        	update_button_array[i] = new JButton("更新");
	        	label_array[i].setText(this.label_name[i]);
	        	
	        	panel.add(label_array[i]);
		        panel.add(text_array[i]);
		        query_button_array[i].addActionListener(this);         
		        update_button_array[i].addActionListener(this); 
		        panel.add(query_button_array[i]);
		        panel.add(update_button_array[i]);
		        
	        }
	        
	        add_button.addActionListener(this);         
	        del_button.addActionListener(this); 
	        batch_button.addActionListener(this); 
	        panel.add(add_button);
	        panel.add(del_button);
	        
	        //输出信息
	        output_label = new JLabel();
	        panel.add(output_label);
	        batch_panel.add(batch_text);
	        batch_panel.add(batch_button);
	        
	        user_num_label.setText("活跃用户数量");
	        insert_num_label.setText("当天新建用户量");
	        update_num_label.setText("当天修改用户量");
	        
	        batch_panel.add(user_num_label);
	        batch_panel.add(user_num_text);
	        batch_panel.add(insert_num_label);
	        batch_panel.add(insert_num_text);
	        batch_panel.add(update_num_label);
	        batch_panel.add(update_num_text);
	        
	        add(panel);	
	        add(batch_panel);
	        
	        setLayout(new FlowLayout()); 
	        
	        setSize(1200,360);  
	        setVisible(true);  
	    }
	    
	    public void actionPerformed(ActionEvent e)  
	    {  
	    	for(int i = 0 ; i < 4; i++)
	    	{
	    		if(e.getSource()==query_button_array[i])
	    		{
	    			String result=""; 
		            try
		            {            	
		            	long startTime=System.currentTimeMillis();  
		            	Method method = service_caller.getClass().getMethod(get_method_name[i],String.class);
		            	result = (String) method.invoke( service_caller , user_id_text.getText()); 
		           
		            	long endTime=System.currentTimeMillis();
		            	output_label.setText("调用时间：" + (endTime-startTime) + " ms");            	
		            }
		            catch(Exception exception)
		            {
		            	output_label.setText("接口调用失败");
		            }
		            
		            text_array[i].setText(result);
	    		}
	    	}
	    	
	    	for(int i = 0 ; i < 4; i++)
	    	{
	    		if(e.getSource()==update_button_array[i])
	    		{
	    			String result=""; 
		            try
		            {            	
		            	long startTime=System.currentTimeMillis();  
		            	Method method = service_caller.getClass().getMethod(update_method_name[i],String.class,String.class,String.class);
		            	int errcode =  (Integer) (method.invoke( service_caller , user_id_text.getText(), text_array[i].getText(),"wjj")); 
		            	long endTime=System.currentTimeMillis();
		            	
		            	String output = "调用时间：" + (endTime-startTime) + " ms";
		            	if(errcode != 0) 
		            		output = output + "更新错误：错误码为 " + errcode;
		            	output_label.setText(output);            	
		            }
		            catch(Exception exception)
		            {
		            	output_label.setText("接口调用失败");
		            }
		            
		            text_array[i].setText(result);
	    		}
	    	}
	    	
	    	if(e.getSource()==add_button)
    		{
	            try
	            {    
	            	int errcode = service_caller.CreateNewUser(user_id_text.getText(), "wjj", text_array[0].getText(), text_array[1].getText(), text_array[2].getText(), text_array[3].getText());
	            	String output = "";
	            	if(errcode != 0) 
	            		output = "更新错误：错误码为 " + errcode;
	            	output_label.setText(output);            	
	            }
	            catch(Exception exception)
	            {
	            	output_label.setText("接口调用失败");
	            }

    		}
	    	
	    	if(e.getSource()==del_button)
    		{
	            try
	            {    
	            	int errcode = service_caller.DeleteUser(user_id_text.getText());
	            	String output = "";
	            	if(errcode != 0) 
	            		output = "更新错误：错误码为 " + errcode;
	            	output_label.setText(output);            	
	            }
	            catch(Exception exception)
	            {
	            	output_label.setText("接口调用失败");
	            }
    		}
	    	
	    	if(e.getSource()==batch_button)
    		{
	            try
	            {    
	            	int errcode = service_caller.BatchUpdateUserStatus(batch_text.getText(),"wjj");
	            	String output = "";
	            	if(errcode != 0) 
	            		output = "更新错误：错误码为 " + errcode;
	            	output_label.setText(output);            	
	            }
	            catch(Exception exception)
	            {
	            	output_label.setText("接口调用失败");
	            }   
	        	
    		}
	       
	    	user_num_text.setText(String.valueOf(service_caller.GetActiveUserNumber()));
	    	update_num_text.setText(String.valueOf(service_caller.GetTodayUpdateNumber()));
	    	insert_num_text.setText(String.valueOf(service_caller.GetTodayInsertNumber()));
	    	repaint();
	    }  
	    
}

class TestWindowListener implements WindowListener{

	  public void windowActivated(WindowEvent e) {
	    // TODO Auto-generated method stub
	    
	  }

	 

	  public void windowClosed(WindowEvent e) {   }

	  public void windowClosing(WindowEvent e) {
	    e.getWindow().dispose();
	    
	  }

	 

	  public void windowDeactivated(WindowEvent e) { }

	  public void windowDeiconified(WindowEvent e) { }

	  public void windowIconified(WindowEvent e) { }

	  public void windowOpened(WindowEvent e) { } 
	  
	}
